function gethandle(autocreate, visible)
{
	this.handle;

	if (autocreate != false)
	{
		autocreate = true;
	}
	
	if (this.handle == null)
	{
		if (autocreate)
		{
			this.handle = WScript.CreateObject("InternetExplorer.Application", "WM_");
			this.handle.Silent = true;
			if (visible == true)
			{
				this.handle.Visible = true;
			}
		}
	}
	else
	{
		if (this.handle.Visible != visible)
		{
			this.handle.Visible = visible;
		}
	}

	return this.handle;
}

function WaitForDocumentComplete(handle)
{
	do
	{
		WScript.Sleep(100);
	} while (handle.Busy || handle.ReadyState != 4);
}

function getdocument(handle, url)
{
	this.document;
	
	if (url != null)
	{
		if (url == handle.LocationURL)
		{
			handle.Refresh();
		}
		else
		{
			handle.navigate(url);
		}
		
		WaitForDocumentComplete(handle);
	}
	
	this.document = handle.document;
	return this.document;
}

function quit(msg)
{
	if (handle = gethandle(false))
	{
		handle.Quit();
	}

	if (msg != null && msg != "")
	{
		WScript.Echo(msg);
	}

	WScript.Quit();
}

function cut(string, from, end)
{
	if (string == "")
	{
		return string;
	}

	f_index = string.indexOf(from);
	e_index = string.indexOf(end);
	
	if (f_index == -1)
	{
		f_index = 0;
	}
	else
	{
		f_index += from.length;
	}

	if (e_index == -1)
	{
		e_index = string.length;
	}

	return string.substring(f_index, e_index);
}

function reg_match(string, reg)
{
	var array = reg.exec(string);
	if (array && array[1])
	{
		return array[1];
	}
	else
	{
		return "";
	}
}

function system(command, windowstyle, waitonreturn)
{
	var shell = WScript.CreateObject("WScript.Shell");

	if (windowstyle == null)
	{
		windowstyle = 1;
	}

	if (waitonreturn == null)
	{
		waitonreturn = true;
	}
	

	shell.run(command, windowstyle, waitonreturn);
}

function setcookie(document, name, value, expireHours, domain, path)
{
	value = escape(value);
	if (expireHours != null && expireHours != 0)
	{
		var date=new Date();
		date.setTime(date.getTime()+expireHours*3600*1000);
		date = date.toGMTString();
		if (domain != null && path != null)
			document.cookie = name + "=" + value + "; domain=" + domain + "; path=" + path + "; expires=" + date;
		else
			document.cookie = name + "=" + value + "; expires=" + date;
	}
	else
	{
		if (domain != null && path != null)
			document.cookie = name + "=" + value + "; domain=" + domain + "; path=" + path;
		else
			document.cookie = name + "=" + value;
	}
}

function emptycookie(document, domain)
{
	var array = document.cookie.split("; ");
	for (n = 0; n < array.length; n++)
	{
		var index = array[n].indexOf("=");
		var name;
		
		if (index != -1)
			name = array[n].substring(0, index);
		else
			name = array[n];

		if (domain == null)
			setcookie(document, name, "", -1);
		else
			setcookie(document, name, "", -1, domain, "/");
	}
}

function xmlhttp(url, method, postdata, additional_header)
{
	var http = new ActiveXObject("MSXML2.XMLHTTP");
	var head = "";
	var content = "";

	if (method == null)
	{
		method = "get";
	}

	method = method.toLowerCase();
	if (method != "get" && method != "post")
	{
		return Array(head, content);
	}

	http.Open(method, url, false);
	http.setRequestHeader("If-Modified-Since", "0");

	if (additional_header != null && additional_header != "")
	{
		var array = additional_header.split("&");
		for (n = 0; n < array.length; n++)
		{
			var key = ":";
			var index = array[n].indexOf(key);
			var name = array[n].substring(0, index);
			var value = array[n].substring(index + key.length, array[n].length);

			if (name != "")
			{
				http.setRequestHeader(name, value);
			}
		}
	}

	if (method == "post")
	{
		http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		http.Send(postdata);
	}
	else
	{
		http.Send();
	}

	head = http.getAllResponseHeaders();
	content = http.ResponseText;

	return Array(head, content);
}

function hash()
{
	var id = "";
	var hash = new Array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z");

	for (loop = 0; loop < 10; loop++)
	{
		rnd = Math.floor(Math.random() * hash.length);
		id = id + hash[rnd];
	}

	return id;
}

function hook()
{
	var fso = new ActiveXObject("Scripting.FileSystemObject");
	var CurrentDir = fso.GetAbsolutePathName(".");
	var script=document.createElement("script");
	script.src= CurrentDir + "\\hook.js";
	document.appendChild(script);
}	

function query_cookie_value(name)
{
	var cookie = getdocument(gethandle(true, true)).cookie;
	var value = "";
	var array = cookie.split("; ");

	for (var n = 0; array[n] != null && array[n] != ""; n++)
	{
		if (array[n].indexOf(name) != -1)
		{
			value = array[n].substring(array[n].indexOf("=") + 1);
			break;
		}
	}

	return value;
}

/* callback function
function WM_OnQuit()
{
	// quit();
}

function WM_DocumentComplete()
{
	;
}
*/

